var express = require("express");
const onSetupWebsocket = require("./routes/websocket/index.cjs");

// 使用cors中间件
// const cors = require("cors");

const app = express();
//项目启动端口
const port = process.env.PORT || "8200";

// 跨域
const setupToolCors = require("./tools/cors.cjs");
setupToolCors(app);

// 缓存中间件
const setupToolCachingMiddleware = require("./tools/cachingMiddleware.cjs");
setupToolCachingMiddleware(app);

// api注册
const apiRouter = require("./routes/api/index");
app.use("/api", apiRouter);
app.use("/hello", (_req, res) => {
  res.send("hello world");
});


// app.use((_req, res) => {
//   res.send("hello world");
// });

// 静态文件挂置 public
app.use(express.static("public"));



// 创建项目 --- 启动HTTP服务器
const server = app.listen(port, () => {
  console.log(`项目启动成功-http://localhost:${port}`);
});

onSetupWebsocket(server);

module.exports = app;

